/*
 * Copyright (C) Francisco Morero Peyrona. All rights reserved.
 *
 * This software is published under the terms of Open Source
 * License version 1.1, a copy of which has been included with this
 * distribution in the License.txt file.
 */

package jdbcmanager.app;

import java.util.*;
import java.io.*;


/**
 * Reads a file specified by constructor argument.
 * <p>
 * This file stores information that has to persist among consecutives
 * executions of the application.
 * <p>
 * To facilitate the use of this class, all methods are static.
 * <p>
 * Keys are case sensitive.
 *
 * @author Francisco Morero Peyrona
 */
public final class AppProperties
{
   //-------------------------------------------------------------------------//
   // Constants stored in "user/jdbcmanager.props" file

   // Look and Feel
   private static final String sKEY_FONT_SIZE           = "laf.FontSize";
   private static final String sKEY_LOOK_AND_FEEL       = "laf.LookFeel";

   // Connection and Tables (sql explorer)
   private static final String sKEY_CONN_LOGIN_TIME_OUT = "conn.login.timeout"; 
   private static final String sKEY_CONN_QUERY_TIME_OUT = "conn.query.timeout";
   private static final String sKEY_DEFAULT_MAX_ROWS    = "table.DefaultMaxRows";
   private static final String sKEY_UNLOCK_SQL_GRID     = "table.UnlockSQLGrid";
 
   // Proxy
   private static final String sKEY_PROXY_HTTP_USE      = "proxy.http.Active";
   private static final String sKEY_PROXY_HTTP_SERVER   = "proxy.http.Server";
   private static final String sKEY_PROXY_HTTP_PORT     = "proxy.http.Port";
   private static final String sKEY_PROXY_HTTP_USER     = "proxy.http.User";
   private static final String sKEY_PROXY_HTTP_PASSWORD = "proxy.http.Password";
   private static final String sKEY_PROXY_HTTP_EXCLUDE  = "proxy.http.Exclude";
   private static final String sKEY_PROXY_SOCKS_USE     = "proxy.socks.Active";
   private static final String sKEY_PROXY_SOCKS_SERVER  = "proxy.socks.Server";
   private static final String sKEY_PROXY_SOCKS_PORT    = "proxy.socks.Port";
   
   // Window
   // Window: Position
   private static final String sKEY_WND_X               = "wnd.X";
   private static final String sKEY_WND_Y               = "wnd.Y";
   private static final String sKEY_WND_WIDTH           = "wnd.Width";
   private static final String sKEY_WND_HEIGHT          = "wnd.Height";
   // Window: Spliters positions   
   private static final String sKEY_SPLIT_TREE_HELP     = "wnd.split.TreeHelp";
   private static final String sKEY_SPLIT_DESKTOP_VERBO = "wnd.split.DesktopVerboser";
   private static final String sKEY_SPLIT_LEFT_RIGHT    = "wnd.split.LeftRight";   

   // Other
   private static final String sKEY_ASK_ON_EXIT         = "app.AskOnExit";
   private static final String sKEY_USE_OPEN_GL         = "app.UseOpenGL";
   private static final String sKEY_START_UPC           = "app.StartUPC";
   private static final String sKEY_SHOW_README         = "app.ShowReadMe";
    
   //-------------------------------------------------------------------------//

   private Properties props = null;
   private File       file  = null;
   
   //-------------------------------------------------------------------------//
  
   /**
    * Instances of this class can be obtained only from inside this package.
    *  
    * @param fProps
    */
   AppProperties( File fProps )
   {
      this.props = new Properties();
      this.file  = fProps;

      try
      {
         this.props.load( new FileInputStream( fProps ) );
      }
      catch( IOException exc )
      {
         // Nothing to do: can't call "App.showDebugException( exc );" because enters in an infinite loop 
      }
   }
   
   //-------------------------------------------------------------------------//

   public String getStartupUPC( String s )         { return getString( sKEY_START_UPC, s );           }
   public void   setStartupUPC( String s )         { set( sKEY_START_UPC, s ); save();                }

   public int  getFontSize( int n )                { return getInt( sKEY_FONT_SIZE, n );              }
   public void setFontSize( int n )                { set( sKEY_FONT_SIZE, n ); save();                }
   
   public String getLookAndFeel( String s )        { return getString( sKEY_LOOK_AND_FEEL, s );       }
   public void   setLookAndFeel( String s )        { set( sKEY_LOOK_AND_FEEL, s ); save();            }

   //------------------------------------------------------------------------------------------------//
   
   public int  getConnLoginTimeOut( int n )        { return getInt( sKEY_CONN_LOGIN_TIME_OUT, n );    }
   public void setConnLoginTimeOut( int n )        { set( sKEY_CONN_LOGIN_TIME_OUT, n ); save();      }
   
   public int  getConnQueryTimeOut( int n )        { return getInt( sKEY_CONN_QUERY_TIME_OUT, n );    }
   public void setConnQueryTimeOut( int n )        { set( sKEY_CONN_QUERY_TIME_OUT, n ); save();      }

   public int  getSQLGridDefaultMaxRows( int n )   { return getInt( sKEY_DEFAULT_MAX_ROWS, n );       }
   public void setSQLGridDefaultMaxRows( int n )   { set( sKEY_DEFAULT_MAX_ROWS, n ); save();         }

   public boolean getSQLGridUnlocked( boolean b )  { return getBoolean( sKEY_UNLOCK_SQL_GRID, b );    }
   public void    setSQLGridUnlocked( boolean b )  { set( sKEY_UNLOCK_SQL_GRID, b ); save();          }
   
   //------------------------------------------------------------------------------------------------//
   
   public boolean getUseOpenGL( boolean b )        { return getBoolean( sKEY_USE_OPEN_GL, b );        }
   public void    setUseOpenGL( boolean b )        { set( sKEY_USE_OPEN_GL, b ); save();              }

   public boolean getAskOnExit( boolean b )        { return getBoolean( sKEY_ASK_ON_EXIT, b );        }
   public void    setAskOnExit( boolean b )        { set( sKEY_ASK_ON_EXIT, b ); save();              }
      
   public int  getSplitTreeHelp( int n )           { return getInt( sKEY_SPLIT_TREE_HELP, n );        }
   public void setSplitTreeHelp( int n )           { set( sKEY_SPLIT_TREE_HELP, n ); save();          }
   
   public int  getSplitDesktopVerbo( int n )       { return getInt( sKEY_SPLIT_DESKTOP_VERBO, n );    }
   public void setSplitDesktopVerbo( int n )       { set( sKEY_SPLIT_DESKTOP_VERBO, n ); save();      }

   public int  getSplitLeftRight( int n )          { return getInt( sKEY_SPLIT_LEFT_RIGHT, n );       }
   public void setSplitLeftRight( int n )          { set( sKEY_SPLIT_LEFT_RIGHT, n ); save();         }
   
   public int  getWndX( int n )                    { return getInt( sKEY_WND_X, n );                  }
   public void setWndX( int n )                    { set( sKEY_WND_X, n ); save();                    }
   
   public int  getWndY( int n )                    { return getInt( sKEY_WND_Y, n );                  }
   public void setWndY( int n )                    { set( sKEY_WND_Y, n ); save();                    }
   
   public int  getWndWidth( int n )                { return getInt( sKEY_WND_WIDTH, n );              }
   public void setWndWidth( int n )                { set( sKEY_WND_WIDTH, n ); save();                }
   
   public int  getWndHeight( int n )               { return getInt( sKEY_WND_HEIGHT, n );             }
   public void setWndHeight( int n )               { set( sKEY_WND_HEIGHT, n ); save();               }
   
   public boolean getProxyHttpActive( boolean b )  { return getBoolean( sKEY_PROXY_HTTP_USE, b );     }
   public void    setProxyHttpActive( boolean b )  { set( sKEY_PROXY_HTTP_USE, b );  save();          }

   public String getProxyHttpServer( String s )    { return getString( sKEY_PROXY_HTTP_SERVER, s );   }
   public void   setProxyHttpServer( String s )    { set( sKEY_PROXY_HTTP_SERVER, s ); save();        }

   public String getProxyHttpPort( String s )      { return getString( sKEY_PROXY_HTTP_PORT, s );     }
   public void   setProxyHttpPort( String s )      { set( sKEY_PROXY_HTTP_PORT, s ); save();          }

   public String getProxyHttpUser( String s )      { return getString( sKEY_PROXY_HTTP_USER, s );     }
   public void   setProxyHttpUser( String s )      { set( sKEY_PROXY_HTTP_USER, s ); save();          }

   public String getProxyHttpPassword( String s )  { return getString( sKEY_PROXY_HTTP_PASSWORD, s ); }
   public void   setProxyHttpPassword( String s )  { set( sKEY_PROXY_HTTP_PASSWORD, s ); save();      }

   public String getProxyHttpExclude( String s )   { return getString( sKEY_PROXY_HTTP_EXCLUDE, s );  }
   public void   setProxyHttpExclude( String s )   { set( sKEY_PROXY_HTTP_EXCLUDE, s ); save();       }

   public boolean getProxySocksActive( boolean b ) { return getBoolean( sKEY_PROXY_SOCKS_USE, b );    }
   public void    setProxySocksActive( boolean b ) { set( sKEY_PROXY_SOCKS_USE, b );  save();         }
   
   public String getProxySocksServer( String s )   { return getString( sKEY_PROXY_SOCKS_SERVER, s );  }
   public void   setProxySocksServer( String s )   { set( sKEY_PROXY_SOCKS_SERVER, s ); save();       }

   public String getProxySocksPort( String s )     { return getString( sKEY_PROXY_SOCKS_PORT, s );    }
   public void   setProxySocksPort( String s )     { set( sKEY_PROXY_SOCKS_PORT, s ); save();         }
     
   public boolean getShowReadMe( boolean b )       { return getBoolean( sKEY_SHOW_README, b );        } 
   public void    setShowReadMe( boolean b )       { set( sKEY_SHOW_README, b ); save();              } 
   
   //-------------------------------------------------------------------------//
   
   // Method used only by App
   boolean isDebugging()
   {
      return getBoolean( "debugging", false );
   }   
   
   //-------------------------------------------------------------------------//
   
   // Calling this method a lot of times does not affect performance because OOSS do
   // not save to disk, but to memory, and when the OS decides, is saved to disk.
   private void save()
   {
      try
      {
         String sHead = " "+ App.sAPP_TITLE + " - Configuration File.\n" +
                        "# Do not modify this file unless you know exactly what you are doing.\n"+
                        "# No modifique este fichero a menos que sepa exactamente lo que hace.";
                         
         FileOutputStream fos = new FileOutputStream( this.file );
         
         props.store( fos, sHead );
                     
         fos.close();
      }
      catch( Exception exc )
      {
         App.showDebugException( exc );
      }
   }
/*
   private String[] getStringArray( String sKey, String sSeparator )
   {
      return App.getStrWise().string2Array( (String) this.props.get( sKey ), sSeparator );
   }
*/
   private String getString( String sKey, String sDefValue )
   {
      return this.props.getProperty( sKey, sDefValue );
   }

   private boolean getBoolean( String sKey, boolean bDefValue )
   {
      String s = this.props.getProperty( sKey );

      return s == null ? bDefValue : s.equalsIgnoreCase( "TRUE" );
   }

   private int getInt( String sKey, int nDefValue )
   {
      String s = this.props.getProperty( sKey );

      return (s == null || s.length() == 0) ? nDefValue : Integer.parseInt( s );
   }
/*
   private long getLong( String sKey, long nDefValue )
   {
      String s = this.props.getProperty( sKey );

      return (s == null) ? nDefValue : Long.parseLong( s );
   }

   private float getFloat( String sKey, float nDefValue )
   {
      String s = this.props.getProperty( sKey );

      return (s == null) ? nDefValue : Float.parseFloat( s );
   }

   private void set( String sKey, String[] asValue, String sSeparator )
   {
      StringBuffer sb = new StringBuffer();

      if( asValue != null )
      {
         for( int i = 0; i < asValue.length; i++ )
            sb.append( asValue[i] ).append( sSeparator );

         if( sb.length() > 0 )
            sb.delete( sb.length() - sSeparator.length(), sb.length() );
      }

      this.props.setProperty( sKey, sb.toString() );
   }
*/
   private void set( String sKey, String sValue )
   {
      this.props.setProperty( sKey, sValue );
   }

   private void set( String sKey, boolean bValue )
   {
      this.props.setProperty( sKey, String.valueOf( bValue ) );
   }

   private void set( String sKey, int nValue )
   {
      this.props.setProperty( sKey, String.valueOf( nValue ) );
   }
/*
   private void set( String sKey, long nValue )
   {
      this.props.setProperty( sKey, String.valueOf( nValue ) );
   }

   private void set( String sKey, float nValue )
   {
      this.props.setProperty( sKey, String.valueOf( nValue ) );
   }
*/
}